package com.demo2do.boyle.client.web.controller.advertisement;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.boyle.client.entity.Advertisement.AdvertisementDto;
import com.demo2do.boyle.client.service.AdvertisementService;
import com.demo2do.boyle.client.service.AdvertisementTagService;
import com.demo2do.boyle.client.service.CustomerService;
import com.demo2do.boyle.client.service.CustomerTagService;
import com.demo2do.boyle.client.service.TaskService;
import com.demo2do.boyle.common.entity.advertisement.Advertisement;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.CustomerTag;
import com.demo2do.boyle.common.entity.customer.social.SocialType;
import com.demo2do.boyle.common.entity.task.Task;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.core.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/advertisements")
public class AdvertisementController {
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired 
	private CustomerTagService customerTagService;
	
	@Autowired
	private AdvertisementService advertisementService;
	
    @Autowired
    private TaskService taskService;
	
	@Autowired
	private AdvertisementTagService advertisementTagService;

    private static final Logger logger = LoggerFactory.getLogger(AdvertisementController.class);
	
    /**
     * 
     * @param customerId
     * @param socialType
     * @param page
     * @return
     */
	@RequestMapping("")
	@ResponseBody
	public String list(@Secure(property = "id") Integer customerId,
					   @RequestParam(value="type", required = false) SocialType socialType, Page page) {
		Result result = new Result();
		
		
		
		try {
			
			Customer customer = customerService.load(Customer.class, customerId);
			
			// one to one so customerId same as customerTag
			CustomerTag customerTag = customerTagService.load(CustomerTag.class, customerId);
			
			List<Task> taskLists = taskService.listByCustomer(customer, null, page);
        	List<Integer> advertiseLists = new ArrayList<Integer>();
        	for (Task task : taskLists) {
        		advertiseLists.add(task.getAdvertisement().getId());
			}
			
			List<Advertisement> advertisements = advertisementTagService.listByCutomerTag(customerTag, page, socialType);
			
			//update by Jason.Y at 2015/12/10 ( this code need to remove !)
			List<Advertisement> advertisementsTest = advertisementService.listByTest(page,socialType);
			advertisements.addAll(advertisementsTest);
			////////////////////////////////////////////////////////////////////
			
			List<AdvertisementDto> advertisementDtos = new ArrayList<>();
			for(Advertisement ad : advertisements) {
				advertisementDtos.add(new AdvertisementDto(ad));
			}
			
			result.success().data("advertisementDtos", advertisementDtos).data("customerTag", customerTag).data("advertisementIds", advertiseLists);
			
		} catch (Exception e) {			
			logger.info("getAdvertisementsByTag()" + e);
			result.fail().message("服务器异常");
		}
		
		return JsonUtils.toJsonString(result);
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}")
	@ResponseBody
	public String getAdvertisement(@PathVariable("id") Integer id) {
		
		Result result = new Result(); 
		
		try {
			
			Advertisement advertisement = advertisementService.load(Advertisement.class, id);
			AdvertisementDto advertisementDto = new AdvertisementDto(advertisement);
			
			result.success().data("advertisementDto", advertisementDto);
			
		} catch (Exception e) {
			logger.info("getAdvetisement()" + e);
			result.fail().message("服务器异常");
		}
		
		return JsonUtils.toJsonString(result);
		
	}
	
    
}
